import './index.less';
import Utils from "../../../common/utils.js";
// 弹窗
import Dialog from '../../../components/h5/dialog';
// 主页
import galleryListView from "./index.html";
import headerPartial from "../index/partial/header.html";
import list from "../index/partial/newslist.html";
import vListPartial from "../live/partial/videoList.html";
// 接口服务
import VideosService from "../../../services/videosService.js";
const videosService = new VideosService();

export default class GalleryList {
    constructor() {
        this.beginScroll = true;
        this.request = true;
        this.galleryContainer = $(".gallery-container");
        this.complied = _.template(galleryListView);
        this.compliedHeaderPartial = _.template(headerPartial);
        this.compliedVideoListPartial = _.template(vListPartial);

        this.compliedList = _.template(list);

        this.token = Utils.GetToken();
        console.log("token是", this.token)
        if (this.token == '') {
            alert("无法获取token")
        }
        this.HospitalRoleName = ''
        this.domain = ''
        this.render();
        this.start = 1;
        this.length = 30;

        this.isMore = true;

    }
    //渲染页面
    async render() {
        let _this = this;
        let hpList = [];
        let dmList = [];

        //领域列表
        let dmResult = await videosService.getDomainList(this.token);
        console.log("dmResult", dmResult)
        if (dmResult.code == 200) {
            dmList = dmResult.data;
            _this.domain = dmList[0].name; //记录第一个领域
        } else {
            alert(dmResult.message)
        }
        //医院列表
        let hpResult = await videosService.getHpList(this.token);
        console.log("hpResult", hpResult)
        if (hpResult.code == 200) {
            hpList = hpResult.data;
        } else {
            alert(hpResult.message)
        }
        //更多视频
        var lable_result_List = [];
        let videoResult = await videosService.getVideoList(this.token, {
            start: 1,
            length: _this.length
        });
        console.log("videoResult", videoResult)
        if (videoResult.code == 200) {
            lable_result_List = videoResult.data;
            if (lable_result_List.length >= _this.length) {
                _this.start = _this.start + 1;
            } else {
                //已无更多数据，不再加载。
                _this.isMore = false;
            }
        } else {
            alert(videoResult.message)
        }

        //加载header
        let headerHtml = _this.compliedHeaderPartial({
            data: 3,
            token: _this.token
        });
        setTimeout(() => {
            $(".header-container").html(headerHtml);
        }, 100);

        //整个页面列表
        let galleryListHtml = this.complied({
            dmList: dmList,
            hpList: hpList,
        });
        $(".content").html(galleryListHtml);
        let vlistHtml = _this.compliedVideoListPartial({
            lable_result_List: lable_result_List.splice(0, _this.length),
        });
        $(".videos").html(vlistHtml);
        if (_this.domain != '') {
            //获取列表
            _this.getlist({
                token: _this.token,
                domain: _this.domain,
            });
        }

        // console.log("data",data)
        this.registerEvents();


    }
    //更新列表1
    async getlist(option) {
        let _this = this;
        var liveList = [];
        let result = await videosService.getLiveList(option);
        console.log("result", result)
        if (result.code == 200) {
            liveList = result.data;
        } else {
            alert(result.message)
        }
        //更新视图
        let List3Html = _this.compliedList({
            liveList3: liveList,
        });
        $(".news_container").html(List3Html);
    }
    //绑定事件
    registerEvents() {
        let _this = this;
        //切换领域
        $('.videos_container').delegate(".dm", "click", async function(e) {
            _this.domain = $(this).attr('data-name')
            var liveList = {};
            $('.dm').removeClass("active");
            $(this).addClass("active");
            console.log("2222")
            console.log($(this).attr('data-name'))
            _this.getlist({
                token: _this.token,
                HospitalRoleName: _this.HospitalRoleName,
                domain: $(this).attr('data-name'),
            });

        });
        //切换医院
        $('.tag_group').delegate(".tag", "click", async function(e) {
            _this.HospitalRoleName = $(this).attr('data-name')
            var liveList = {};
            $('.tagLive').removeClass("active");
            $(this).addClass("active");
            var params = '';
            if ($(this).attr('data-name') == "all") {
                params = "";
            } else {
                params = $(this).attr('data-name');
            }

            _this.getlist({
                token: _this.token,
                HospitalRoleName: params,
                domain: _this.domain,
            });

        });

        //点击顶部新闻
        $('.videos_container').delegate(".news", "click", function() {
            var id = $(this).attr('data-id')
            // if ($(this).attr('data-status') == 'end') {
            window.location = `/video.html?id=${id}&token=${_this.token}`
            // } else {
            // window.location = `/live.html?id=${id}&token=${_this.token}`
            // }
        });

        //点击更多视频
        $('.videos_container').delegate(".videoItem", "click", function() {
            var id = $(this).attr('data-id')
            if ($(this).attr('data-status') == 'end') {
                window.location = `/video.html?id=${id}&token=${_this.token}`
            } else {
                window.location = `/live.html?id=${id}&token=${_this.token}`
            }

        });






        function debounce(fn, wait) {
            var timeout = null;
            return function() {
                if (timeout !== null) clearTimeout(timeout);
                timeout = setTimeout(fn, wait);
            }
        }

        async function handle() {
            console.log("jiazai11")
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $(document).height();
            var windowHeight = $(this).height();
            if (scrollTop + windowHeight + 5 >= scrollHeight) {

                console.log("jiazai")
                if (_this.isMore == false) {
                    console.log("无需加载了")
                    return;
                }
                let videoResult = await videosService.getVideoList(this.token, {
                    start: _this.start,
                    length: _this.length
                });
                console.log("videoResult", videoResult)
                if (videoResult.code == 200) {
                    lable_result_List = videoResult.data;
                    if (lable_result_List.length >= _this.length) {
                        _this.start = _this.start + 1;
                    } else {
                        //已无更多数据，不再加载。
                        _this.isMore = false;
                    }
                } else {
                    alert(videoResult.message)
                }

                let vlistHtml = _this.compliedVideoListPartial({
                    lable_result_List: lable_result_List,
                });
                $(".videos").append(vlistHtml);
            }
        }
        //滚动加载
        window.addEventListener('scroll', debounce(handle, 300));





    }



}